/**
 * 
 */
package com.zufangbao.earth.web.controller.assets;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.utils.StringUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;
import com.zufangbao.sun.entity.customer.Customer;
import com.zufangbao.sun.service.CustomerService;

/**
 * @author Downpour
 */
@Controller
@RequestMapping("/renters")
@MenuSetting("menu-data")
public class RenterController {
	
	@Autowired
	private CustomerService customerService;
	/**
	 * List app renters
	 * 
	 * @return
	 */
	@RequestMapping("")
	@MenuSetting("submenu-assets-renter")
	public ModelAndView list(Page page) {
		
		List<Customer> customerList = customerService.list(Customer.class, page);
		
		ModelAndView modelAndView = new ModelAndView("renter/renter-list");
		modelAndView.addObject("customerList", customerList);
		modelAndView.addObject("page", page);

		return modelAndView;
	}
	
	/**
	 * List app renter detail
	 * 
	 * @return
	 */
	@RequestMapping("/{renterId}/detail")
	public ModelAndView detail(@PathVariable("renterId") Long renterId) {
		
		Customer customer = customerService.load(Customer.class, renterId);
		
		ModelAndView modelAndView = new ModelAndView("renter/renter-detail");
		modelAndView.addObject("customer", customer);
		
		return modelAndView;
		
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/search")
	public ModelAndView search() {

		ModelAndView modelAndView = new ModelAndView("renter/renter-search");

		return modelAndView;
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/search", method = RequestMethod.GET, params = "filter")
	@MenuSetting("submenu-assets-renter")
	public ModelAndView onSearch(HttpServletRequest request,
							     @RequestParam(value = "name", required = false) String name,
							     @RequestParam(value = "mobile", required = false) String mobile,
							     Page page) {

		ModelAndView modelAndView = new ModelAndView("renter/renter-list");
	
		// create Filter first
		Filter filter = new Filter();
		if(!name.isEmpty()){
			filter.addEquals("name", name);
		}
		if(!mobile.isEmpty()){
			filter.addEquals("mobile", mobile);
		}

		List<Customer> customerList = customerService.list(Customer.class, filter, page);

		// handle query string
		String queryString = request.getQueryString();
		Map<String, String> queries = StringUtils.parseQueryString(queryString);
		if(queries.containsKey("page")) {
			queries.remove("page");
		}
		
		modelAndView.addObject("customerList", customerList);
		modelAndView.addObject("queryString", StringUtils.toQueryString(queries));
		
		return modelAndView;
	}
	
}
